<?php
//session_name(DicomScience);
//session_start();

/*
 * The readReports.php Controller listens on 
 * http_get option "lastdays" a countable integer number.
 * OR http_get option "report" an interger which represents the report id
 * OR http_get option "search" a string which represents a seach on all elements in the table report
 * 
 * The controller always responds in an XML coded http - Datastream. 
 */

$lastDays = $_GET['lastdays'];
$reportNumber = $_GET['report'];
$searchString = $_GET['search'];
$editorId = $_GET['editor'];

$now = time();
$oneday = 86400;
$range = $lastdays * $oneday;
$lastdate = $now - $range;

/*
 * ACTION TRIGGER: which response is appropriate?
 */
if (! $lastDays == ""){
	# See Section GETREPORTLIST
	getReportList($lastDays);
} elseif (! $reportNumber == ""){
	# See Section GETREPORTBYNUMBER
	getReportByNumber($reportNumber);
} elseif (! $searchString == ""){
	# See Section SEARCHREPORT
	getReportBySearch($searchString);
} elseif (! $editorId == ""){
	# See Section GETREPORTSBYEDITOR
	getReportsByEditor($editorId);
}

/*
 * SECTION GETREPORTBYEDITOR
 */
function getReportsByEditor(){
	
	$lastDays = $_GET['lastdays'];
	$reportNumber = $_GET['report'];
	$searchString = $_GET['search'];
	$editorId = $_GET['editor'];
	
	$now = time();
	$oneday = 86400;
	$range = $lastDays * $oneday;
	$lastdate = $now - $range;
	
	require('../includes/gset.php');
	
	include ('../includes/database/connect.php');
	$sql_getltrep = "SELECT reports.id_report, reports.headline, reports.blogline, reports.diagnosis, reports.last_modified, reports.id_staff, reports.images, reports.id_case, reports.status, staff.firstname, staff.lastname FROM reports, staff WHERE reports.id_staff = staff.id_staff AND reports.id_staff = '$editorId' ORDER BY (last_modified) DESC;";	
	$getltrep = mysql_query($sql_getltrep);
	include ('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<reportList>";

	while ($rep = mysql_fetch_object($getltrep)){
		$idrep = $rep->id_report;
		$headline = $rep->headline;
		$diagnosis = $rep->diagnosis;
		$lastmodified = $rep->last_modified;
		$editorId = $rep->id_staff;
		$editor = $rep->editor;
		$imageavailable = $rep->images;
		$id_case = $rep->id_case;
		$repstat = $rep->status;
		$repfirstname = $rep->firstname;
		$replastname = $rep->lastname;
		
		$date = getDisplayDate($lastmodified);
		
		echo "<report>";
			echo "<reportId>";
			echo $idrep;
			echo "</reportId>";
			echo "<headline>";
			echo $headline;
			echo "</headline>";
			echo "<diagnosis>";
			echo $diagnosis;
			echo "</diagnosis>";
			echo "<editor>";
			echo $repfirstname." ".$replastname;
			echo "</editor>";
			echo "<editorId>";
			echo $editorId;
			echo "</editorId>";
			echo "<status>";
			echo $repstat;
			echo "</status>";			
			echo "<lastModified>";
			echo $date;
			echo "</lastModified>";
		echo "</report>";	
	}
	echo "</reportList>";

}


/*
 * Function getDisplayDate requires the blogdate integer and transforms the integer into a nls specific text.
 */
function getDisplayDate($blogdate) {
	
	require('../includes/gset.php');
	
	// Defining response language
	$session_language = $_SESSION['lang'];
	if ($session_language == "") {
	
		// Assuming Controller has not been triggered by a PHP Session
		if ($lang == 1)	{
			require ('../includes/language/german.php');	
		} elseif($lang == 2){
			require ('../includes/language/english.php');
		} elseif($lang == 3){
			require ('../includes/language/french.php');
		}
			
	}
	
	$aktuelle_zeit = time();
	$lessoneminute = $aktuelle_zeit - 60;
	$lessonehour = $aktuelle_zeit - 3600;
	$lessoneday = $aktuelle_zeit - 86400;
	$yesterday = $aktuelle_zeit - 172800;
	$week = $aktuelle_zeit - 604800;
	$twoweeks = $aktuelle_zeit - 1209600;
	$month = $aktuelle_zeit - 2419200;
	$twomonths = $aktuelle_zeit - 4838400;
	$halfyear = $aktuelle_zeit - 14515200;
	$year = $aktuelle_zeit - 29030400;	
					
	if ($blogdate > $lessoneminute){
	$display_blogdate = $nls_lessoneminute;
	} elseif ($blogdate > $lessonehour){
	$display_blogdate = $nls_lessonehour;
	} elseif ($blogdate > $lessoneday){
	$display_blogdate = $nls_lessoneday;
	} elseif ($blogdate > $yesterday) {
	$display_blogdate = $nls_yesterday;
	} elseif ($blogdate > $week){
	$display_blogdate = $nls_week;
	} elseif ($blogdate > $twoweeks){
	$display_blogdate = $nls_twoweeks;
	} elseif ($blogdate > $month){
	$display_blogdate = $nls_month;
	} elseif ($blogdate > $twomonths){
	$display_blogdate = $nls_twomonths;
	} elseif ($blogdate > $halfyear){
	$display_blogdate = $nls_halfyear;
	} elseif ($blogdate > $year){
	$display_blogdate = $nls_year;
	} else {
	$display_blogdate = $nls_older;
	}

	return $display_blogdate;
}

/*
 * SECTION GETREPORTLIST
 */
function getReportList(){
	
	$lastDays = $_GET['lastdays'];
	$reportNumber = $_GET['report'];
	$searchString = $_GET['search'];

	$now = time();
	$oneday = 86400;
	$range = $lastDays * $oneday;
	$lastdate = $now - $range;
	
	require('../includes/gset.php');
	
	include ('../includes/database/connect.php');
	$sql_getltrep = "SELECT reports.id_report, reports.headline, reports.blogline, reports.diagnosis, reports.last_modified, reports.id_staff, reports.images, reports.id_case, reports.status, staff.firstname, staff.lastname FROM reports, staff WHERE status = '5' AND reports.id_staff = staff.id_staff AND last_modified > $lastdate ORDER BY (last_modified) DESC;";	
	$getltrep = mysql_query($sql_getltrep);
	include ('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<reportList>";

	while ($rep = mysql_fetch_object($getltrep)){
		$idrep = $rep->id_report;
		$headline = $rep->headline;
		$diagnosis = $rep->diagnosis;
		$lastmodified = $rep->last_modified;
		$editorId = $rep->id_staff;
		$editor = $rep->editor;
		$imageavailable = $rep->images;
		$id_case = $rep->id_case;
		$repstat = $rep->status;
		$repfirstname = $rep->firstname;
		$replastname = $rep->lastname;
		
		$date = getDisplayDate($lastmodified);
		
		echo "<report>";
			echo "<reportId>";
			echo $idrep;
			echo "</reportId>";
			echo "<headline>";
			echo $headline;
			echo "</headline>";
			echo "<diagnosis>";
			echo $diagnosis;
			echo "</diagnosis>";
			echo "<editor>";
			echo $repfirstname." ".$replastname;
			echo "</editor>";
			echo "<editorId>";
			echo $editorId;
			echo "</editorId>";
			echo "<status>";
			echo $repstat;
			echo "</status>";			
			echo "<lastModified>";
			echo $date;
			echo "</lastModified>";
		echo "</report>";	
	}
	echo "</reportList>";

}

/*
 * SECTION GETREPORTBYNUMBER
 */
function getReportByNumber(){

	$reportNumber = $_GET['report'];
	
	require('../includes/gset.php');
	
	include ('../includes/database/connect.php');
	$sql_getltrep = "SELECT reports.id_report, reports.headline, reports.blogline, reports.diagnosis, reports.report, reports.last_modified, reports.id_staff, reports.images, reports.id_case, reports.status, staff.firstname, staff.lastname FROM reports, staff WHERE reports.id_staff = staff.id_staff AND reports.id_report = $reportNumber GROUP BY id_report;";	
	$getltrep = mysql_query($sql_getltrep);
	include ('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<reportList>";
	
	while ($rep = mysql_fetch_object($getltrep)){
		$idrep = $rep->id_report;
		$headline = $rep->headline;
		$diagnosis = $rep->diagnosis;
		$text = $rep->report;
		$lastmodified = $rep->last_modified;
		$editor = $rep->editor;
		$editorId = $rep->id_staff;
		$imageavailable = $rep->images;
		$id_case = $rep->id_case;
		$repstat = $rep->status;
		$repfirstname = $rep->firstname;
		$replastname = $rep->lastname;
		
		$date = getDisplayDate($lastmodified);
		
		echo "<report>";
			echo "<reportId>";
			echo $idrep;
			echo "</reportId>";
			echo "<headline>";
			echo $headline;
			echo "</headline>";
			echo "<diagnosis>";
			echo $diagnosis;
			echo "</diagnosis>";
			echo "<reportText>";
			echo $text;
			echo "</reportText>";
			echo "<editor>";
			echo $repfirstname." ".$replastname;
			echo "</editor>";
			echo "<editorId>";
			echo $editorId;
			echo "</editorId>";
			echo "<status>";
			echo $repstat;
			echo "</status>";
			echo "<lastModified>";
			echo $date;
			echo "</lastModified>";
		echo "</report>";	
	}
	echo "</reportList>";
	
}

/*
 * SECTION SEARCHREPORTS
 */
function getReportBySearch(){
	
	$searchString = $_GET['search'];
	
	require('../includes/gset.php');
	
	include ('../includes/database/connect.php');
	$sql_getltrep = "SELECT reports.id_report, reports.headline, reports.blogline, reports.diagnosis, reports.report, reports.last_modified, reports.id_staff, reports.images, reports.id_case, reports.status, staff.firstname, staff.lastname FROM reports, staff WHERE status = '5' AND reports.id_staff = staff.id_staff AND (headline like '%$searchString%' OR diagnosis like '%$searchString%' or report like '%$searchString%' or staff.firstname like '%$searchString%' or staff.lastname like '%$searchString%') GROUP BY id_report ORDER BY last_modified DESC;";	
	$getltrep = mysql_query($sql_getltrep);
	include ('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<reportList>";
	
	while ($rep = mysql_fetch_object($getltrep)){
		$idrep = $rep->id_report;
		$headline = $rep->headline;
		$diagnosis = $rep->diagnosis;
		$text = $rep->report;
		$lastmodified = $rep->last_modified;
		$editor = $rep->editor;
		$editorId = $rep->id_staff;
		$imageavailable = $rep->images;
		$id_case = $rep->id_case;
		$repstat = $rep->status;
		$repfirstname = $rep->firstname;
		$replastname = $rep->lastname;
		
		$date = getDisplayDate($lastmodified);
		
		echo "<report>";
			echo "<reportId>";
			echo $idrep;
			echo "</reportId>";
			echo "<headline>";
			echo $headline;
			echo "</headline>";
			echo "<diagnosis>";
			echo $diagnosis;
			echo "</diagnosis>";
			echo "<reportText>";
			echo $text;
			echo "</reportText>";
			echo "<editor>";
			echo $repfirstname." ".$replastname;
			echo "</editor>";
			echo "<editorId>";
			echo $editorId;
			echo "</editorId>";
			echo "<status>";
			echo $repstat;
			echo "</status>";
			echo "<lastModified>";
			echo $date;
			echo "</lastModified>";
		echo "</report>";	
	}
	echo "</reportList>";
	
}

?>